import java.util.*;

/**
 * ALK.class
 * LeetCode 算法练习
 * @author tingwen
 * @date date
 */
public class ALK {
    public static void main(String args[]) {
        int[] nums = new int[]{0, 0, 1, 1, 1, 2, 2, 3, 3, 4};
        // nums 是以“引用”方式传递的。也就是说，不对实参做任何拷贝
        int len = removeDuplicates(nums);

// 在函数里修改输入数组对于调用者是可见的。
// 根据你的函数返回的长度, 它会打印出数组中该长度范围内的所有元素。
        for (int i = 0; i < len; i++) {
            System.out.println("nums = " + nums[i]);
        }


        int[] n = new int[]{1, 2, 3, 4, 5, 6, 7};
        rotate(n, 3);

        int[] maxProfit = new int[]{7, 1, 5, 3, 6, 4};
        maxProfit(maxProfit);
        int[] singleNumber = new int[]{4, 1, 2, 1, 2};
        singleNumber(singleNumber);
    }

    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int complement = target - nums[i];
            if (map.containsKey(complement)) {
                return new int[] { map.get(complement), i };
            }
            map.put(nums[i], i);
        }
        throw new IllegalArgumentException("No two sum solution");
    }

    public void moveZeroes(int[] nums) {
        int index = 0;
        int len = 0;
        for (int i = 0; i < nums.length - 1; i++) {
            if (nums[i] == 0) {
                index = i;
                len++;

            }
        }
    }

    public int[] plusOne(int[] digits) {
        return plus(digits, digits.length - 1);
    }

    public int[] plus(int[] digits, int index) {
        digits[index]++;
        if (digits[index] == 10) {
            digits[index] = 0;
            if (index == 0) {
                int[] t = new int[digits.length + 1];
                System.arraycopy(digits, 0, t, 1, digits.length);
                t[0] = 1;
                return t;
            }
            return plus(digits, index - 1);
        } else {
            return digits;
        }
    }

    public static int[] intersect(int[] nums1, int[] nums2) {

        Map<Integer, Integer> map = new HashMap<>();
        List<Integer> list = new ArrayList<>();
        if (nums1.length == 0) {
            return nums1;
        }
        if (nums2.length == 0) {
            return nums2;
        }
        if (nums1.length > nums2.length) {
            for (int i = 0; i < nums2.length; i++) {
                if (!map.containsKey(nums2[i])) {
                    map.put(nums2[i], 1);
                } else {
                    map.put(nums2[i], map.get(nums2[i]) + 1);
                }

            }
            for (int j = 0; j < nums1.length; j++) {
                if (map.containsKey(nums1[j]) && map.get(nums1[j]) > 0) {
                    list.add(nums1[j]);
                    map.put(nums1[j], map.get(nums1[j]) - 1);
                }
            }
        } else {
            for (int i = 0; i < nums1.length; i++) {
                if (!map.containsKey(nums1[i])) {
                    map.put(nums1[i], 1);
                } else {
                    map.put(nums1[i], map.get(nums1[i]) + 1);
                }

            }
            for (int j = 0; j < nums2.length; j++) {
                if (map.containsKey(nums2[j]) && map.get(nums2[j]) > 0) {
                    list.add(nums2[j]);
                    map.put(nums2[j], map.get(nums2[j]) - 1);
                }
            }
        }
        int[] res = new int[list.size()];
        for (int k = 0; k < list.size(); k++) {
            res[k] = list.get(k);
        }
        return res;
    }

    public static int singleNumber(int[] nums) {
        int len = nums.length;
        int result = 0;
        for (int i = 0; i < len; i++) {
            result ^= nums[i];
        }
        return result;
    }

    public boolean containsDuplicate(int[] nums) {
        Set<Integer> set = new HashSet();
        for (int i = 0; i < nums.length; i++) {
            if (set.contains(nums[i])) {
                return true;
            } else {
                set.add(nums[i]);
            }
        }
        return false;
    }

    public static int maxProfit(int[] prices) {
        int maxprofit = 0;
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] > prices[i - 1])
                maxprofit += prices[i] - prices[i - 1];
        }
        return maxprofit;
    }


    public static int removeDuplicates(int[] nums) {
        Set<Integer> set = new LinkedHashSet<>();
        for (int i = 0; i < nums.length; i++) {
            set.add(nums[i]);
        }
        Iterator iterator = set.iterator();
        int i = 0;
        while (iterator.hasNext()) {
            nums[i] = (int) iterator.next();
            i++;
        }
        return i;
    }


    public static void rotate(int[] nums, int k) {
        int len = nums.length;
        int[] n = nums.clone();
        for (int i = 0; i < nums.length; i++) {
            nums[(i + k) % len] = n[i];
        }
    }


}
